package com.hbisedm.interninfosys.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hbisedm.interninfosys.intern.entity.LoginUser;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录过滤器
 */
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {



    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response) throws AuthenticationException {
        if(!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        /**
         * 从输入流中获取到登录的信息
         *JSON处理
         */
        if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE) || request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE)) {
            try {
                LoginUser loginUser = new ObjectMapper().readValue(request.getInputStream(), LoginUser.class);
//            rememberMe.set(loginUser.getRememberMe() == null ? 0 : loginUser.getRememberMe());
                return this.getAuthenticationManager().authenticate(
                        new UsernamePasswordAuthenticationToken(loginUser.getUsername(), loginUser.getPassword())
                );
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        /**
         * Post处理
         */
        else {
            return super.attemptAuthentication(request, response);
        }
    }
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        if (!request.getMethod().equals("POST")) {
//            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
//        }
//        //json传递
//        if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
//            Map<String, String> loginData = new HashMap<>();
//            try {
//                loginData = new ObjectMapper().readValue(request.getInputStream(), Map.class);
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//
//            }
//            String username = loginData.get(getUsernameParameter());
//            String password = loginData.get(getPasswordParameter());
//            if (username == null) {
//                username = "";
//            }
//            if (password == null) {
//                password = "";
//            }
//            username = username.trim();
//            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
//            this.setDetails(request, authRequest);
//            return this.getAuthenticationManager().authenticate(authRequest);
//        } else {
//            //键值对传递
//            return super.attemptAuthentication(request, response);
//        }
//    }



}
